package com.qf.electronic.mall.email.service.controller;


import com.qf.constant.RedisConstant;
import com.qf.dto.ResultBean;
import com.qf.entity.TUser;
import com.qf.mapper.TUserMapper;
import com.qf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

@Controller
@RequestMapping("email")
public class ActiveController {
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("active")
    @ResponseBody
    public ResultBean activeAccount(String uuid){
       //组织Redis中的键
        String redisKey = StringUtil.getRedisKey(RedisConstant.REGISTER_EMAIL, uuid);
        //得到邮箱账号
        String email = (String)redisTemplate.opsForValue().get(redisKey);
        Example example = new Example(TUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email",email);
        TUser user = new TUser();
        user.setEmail(email);
        user.setFlag(1);
        int i = tUserMapper.updateByExampleSelective(user, example);
        if(i>0){
            return ResultBean.fail("激活失败");
        }
        return ResultBean.ok("激活成功");
    }
}
